﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Lync.Widget.Library.Presentation.Entities;
using Lync.Widget.Library.Presentation.Models;
using Lync.Widget.Library.Presentation.Presenters;

namespace Lync.Widget.Library.Presentation.Views
{
    public class LanguagePacksView : UserControl, ILanguagePacksView
    {

        #region Controls

        protected DropDownList SupportedLanguagesDropdownlist;
        protected Repeater LanguageResourceRepeater;

        protected PlaceHolder LanguagePacksResultPlaceHolder;        
        protected Label ErrorMesssageLabel;
        protected Button SaveButton;
        protected PlaceHolder SavedPlaceHolder;
        protected Label SavedLabel;
        protected Button DownloadButton;

        protected TextBox LanguageResourceNameTextBox;
        protected TextBox LanguageResourceKeyTextBox;
        protected TextBox LanguageResourceValueTextBox;
        protected HiddenField CurrentLanguageHiddenField;
        protected HiddenField DefaultLanguageHiddenField;
        protected TextBox NewLanguageTextBox;

        #endregion


        private ILanguagePacksPresenter m_Presenter;



        #region View Properties


        public IList<ILanguage>  SupportedLanguages
        {
            get
            {
                return SupportedLanguagesDropdownlist.Items.Cast<ListItem>().Select(l => new Language
                                                                                             {
                                                                                                 Name = l.Text,
                                                                                                 Value = l.Value
                                                                                             }).Cast<ILanguage>().ToList();
            }
            set
            {
                SupportedLanguagesDropdownlist.Items.Clear();
                value.ToList().ForEach(l => SupportedLanguagesDropdownlist.Items.Add(new ListItem(l.Name, l.Value)));                
            }
        }

        public string NewLanguage
        {
            get { return NewLanguageTextBox.Text; }
            set { NewLanguageTextBox.Text = value; }
        }

        public string Language
        {
            get { return SupportedLanguagesDropdownlist.SelectedValue; }
            set { SupportedLanguagesDropdownlist.SelectedValue = value; }
        }

        public string CurrentLanguage
        {
            get { return CurrentLanguageHiddenField.Value; }
            set { CurrentLanguageHiddenField.Value = value; }
        }

        public IList<ILanguageResource> Resources
        {
            get
            {
                var temp = new List<ILanguageResource>();
                FillResources(temp);
                return temp;
            }
            set
            {
                LanguageResourceRepeater.DataSource = value;
                LanguageResourceRepeater.DataBind();
            }
        }
        public bool Saved
        {
            set
            {
                SavedPlaceHolder.Visible = value;
                ErrorMessage = null;
            }
        }
        public ILanguageResource NewLanguageResource
        {
            get
            {                
                return new LanguageResource
                           {
                               Name = LanguageResourceNameTextBox.Text,
                               Key = LanguageResourceKeyTextBox.Text,
                               Value = LanguageResourceValueTextBox.Text,
                           }; 
            }            
        }
        public bool IsDefaultLanguage
        {
            get { return Convert.ToBoolean(DefaultLanguageHiddenField.Value); }
            set { DefaultLanguageHiddenField.Value = value.ToString(); }
        }

        public void AddToList(ILanguageResource newLanguageResource)
        {
            if (newLanguageResource != null)
            {
                var resources = Resources;
                resources.Add(newLanguageResource);                
                Resources = resources.OrderBy(r => r.Name).ThenBy(r => r.Key).ToList(); ;
            }            
        }

        public bool ShowDownloadButton
        {
            set { DownloadButton.Visible = value; }
        }
        public bool ShowResults
        {
            set { LanguagePacksResultPlaceHolder.Visible = value; }
        }
        public bool ShowSaveButton
        {
            set { SaveButton.Visible = value; }
        }
        public string ErrorMessage
        {
            get { return ErrorMesssageLabel.Text; }
            set
            {
                ErrorMesssageLabel.Text = value;
                ErrorMesssageLabel.Visible = !String.IsNullOrEmpty(value);
            }
        }

        #endregion


        protected override void OnInit(System.EventArgs e)
        {
            m_Presenter = new LanguagePacksPresenter(this, new LanguagePacksModel());
        }

        protected override void OnLoad(System.EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                m_Presenter.OnLoad();
            }
        }

        protected void RemoveButton_OnClick(object sender, EventArgs e)
        {
            var button = (Button)sender;
            var name = button.CommandName;
            var key = button.CommandArgument;

            if (m_Presenter.RemoveLanguageResource(key, name))
            {
                m_Presenter.UpdateView();
            }
        }

        protected void AddLanguageResourceButton_OnClick(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                if (m_Presenter.AddLanguageResource())
                {
                    m_Presenter.UpdateView();
                }                           
            }
        }

        protected void SaveButton_OnClick(object sender, EventArgs e)
        {
            if (Page.IsValid)
            {
                if (m_Presenter.Save())
                {
                    m_Presenter.UpdateView();
                }
            }            
        }

        private void FillResources(List<ILanguageResource> resources)
        {
            var items = (from item in LanguageResourceRepeater.Items.Cast<RepeaterItem>().ToList()
                         select new LanguageResource
                         {
                             Name = ((TextBox)item.FindControl("LanguageResourceNameTextBox")).Text,
                             Key = ((TextBox)item.FindControl("LanguageResourceKeyTextBox")).Text,
                             Value = ((TextBox)item.FindControl("LanguageResourceValueTextBox")).Text,                             
                         });

            resources.AddRange(items);
        }
        protected void DownloadButton_OnClick(object sender, EventArgs e)
        {
            m_Presenter.DownloadResourceFile(HttpContext.Current);
        }

        protected void ChangeLanguageButton_OnClick(object sender, EventArgs e)
        {
            m_Presenter.ChangeLanguage();
        }

        protected void AddLanguageButton_Click(object sender, EventArgs e)
        {
            m_Presenter.AddLanguage();
        }
    }
}